############################################### File notes #######################################

############################### Clean workspace and define settings ##############################


### Run script to set up environment
rm(list=ls())


### Load packages if not already installed
install.packages("K:/BE/1261/Cases/Walgreens_Rite Aid_1610026/Research/Brian/Code/Code/R Packages/lfe_2.5-1998.zip", repos = NULL, type = "win.binary")
install.packages("K:/BE/1261/Cases/Walgreens_Rite Aid_1610026/Research/Brian/Code/R Packages/purrr_0.2.4.zip", repos = NULL, type = "win.binary")
install.packages("K:/BE/1261/Cases/Walgreens_Rite Aid_1610026/Research/Brian/Code/R Packages/tidyselect_0.2.3.zip", repos = NULL, type = "win.binary")
install.packages("K:/BE/1261/Cases/Walgreens_Rite Aid_1610026/Research/Brian/Code/R Packages/tidyr_0.7.2.zip", repos = NULL, type = "win.binary")
library(openxlsx)
library(foreign)
library(Matrix)
library(lfe)
library(data.table)
library(sp)
library(maps)
library(maptools)
library(ggplot2)
library(xtable)
library(lattice)
library(survival)
library(Formula)
library(Hmisc)
library(doBy)
library(readr)
library(tools)
library(tidyr)
library(readstata13)

dataIn <- file.path("K:","BE","1257","Projects","Medicare Claims","HospRetro","Drafts","Health_outcomes","FigureCreation","PSPatients","RefereeResponse","MakeTables")

############################################### create functions for later use ################ 
options(scipen = 999)
#Readin function
 fn.readin <- function(x){
   z <- read.dta13(paste0(dataIn,"/",x)) #dta. files
   z$dataname <- as.character(x) #Use the file name to determine sex results 
   z$sex <- as.character(x) #Use the file name to determine sex results 
   z$sex <- gsub(".dta","",gsub("_overall.dta","",z$sex)) 
   z$cond  <- gsub("fembeta","",gsub("malebeta","",z$sex)) #Use the file name to determine outcomes 
   z$cond  <- gsub("femblock","",gsub("maleblock","",z$cond)) #Use the file name to determine outcomes 
   z$sex  <- gsub("betahyp","",gsub("betadiab","",z$sex))
   z$sex  <- gsub("betaHyp","",gsub("betaDiab","",z$sex))
   z$sex  <- gsub("blockdeath","",gsub("betadeath","",z$sex))
   z$sex  <- gsub("blockDeath","",gsub("betaDeath","",z$sex))
   z$cond[z$cond=="hyp"]  <- "Hypertension"
   z$cond[z$cond=="Hyp"]  <- "Hypertension"
   z$cond[z$cond=="diab"]  <- "Diabetes"
   z$cond[z$cond=="Diab"]  <- "Diabetes"
   return(z)
 }

#OLS FUNCTIONS
 
 
 fn.rsq <- function(x){
   for(i in names(x)[grepl("rn",names(x))] )  {
     x$flag[x[,c(i)]=="R2" ]  <- 1
     x$flag[x[,c(i)]=="N" ]  <- 1
     x$flag[x[,c(i)]=="aR2" ]  <- 1
   }
   y <- names(x)[grepl("beta",names(x))]
   x <- x[x$flag ==1 & !is.na(x$flag),]
   x <- x[,c(names(x)[grepl("beta",names(x))],names(x)[grepl("cond",names(x))] ,names(x)[grepl("sex",names(x))] ,names(x)[names(x)==paste0("rn",gsub("beta_","",names(x)[1]))])]
   x <- reshape(x,idvar =  c(names(x)[grepl("rn",names(x))],names(x)[grepl("cond",names(x))] ,names(x)[grepl("sex",names(x))]) ,direction="long", varying=names(x)[grepl("beta_",names(x))] ,timevar = "ov", v.names = "Statistic")
   for (i in 1:length(y)) {
     x$oc[x$ov==i] <- y[i]
     x$oc <-gsub("_","",gsub("beta_","",x$oc))
     row.names(x) <- NULL
   }
   return(x)
 }   
  
 
 #Execute the readin
 
 #Diff-in-Diff results
 files_ols <- dir(dataIn)[(grepl("fembeta",dir(dataIn)) | grepl("malebeta",dir(dataIn))) & !grepl("Block",dir(dataIn)) & !grepl("IHD",dir(dataIn)) & !grepl("ihd",dir(dataIn)) & !grepl("_overall",dir(dataIn)) & !grepl("block",dir(dataIn))]
 ols.data <- lapply(files_ols , fn.readin )

 rsq <- lapply(ols.data,fn.rsq)
 rsq <- lapply(rsq,function(x) { names(x) <- c("cond","sex","Stat","ov","value","oc") 
               return(x)})
 
 ymean <- rbindlist(rsq) 
 ymean$ov <- NULL
 didymean <- read.xlsx(paste0(dataIn,"/YMeans_diffindiff.xlsx"))
 names(didymean) <- c("oc","cond","sex","Stat","value")

 y <- rbind(ymean,didymean )

 y$oc[y$oc=="death"] <- "Mortality"
 y$oc[y$oc=="diabcomp1"] <- "Asymptomatic"
 y$oc[y$oc=="dc1"] <- "Asymptomatic"
 y$oc[y$oc=="diabcomp2"] <- "Symptomatic"
 y$oc[y$oc=="dc2"] <- "Symptomatic"
 y$oc[y$oc=="glaucoma"] <- "Glaucoma"
 y$oc[y$oc=="ihd"] <- "Ischemic heart"
 y$oc[y$oc=="ami"] <- "AMI"
 y$oc[y$oc=="acuteicd8"] <- "Acute cardiac"
 y$oc[y$oc=="acute cardiac"] <- "Acute cardiac"
 y$sex[y$sex=="fem"] <- "WOMEN"
 y$sex[y$sex=="female"] <- "WOMEN"
 y$sex[y$sex=="male"] <- "MEN"
 y$cond[y$cond=="diabetes"] <- "Diabetes"
 y$cond[y$cond=="hypertension"] <- "Hypertension"
 y$cond[y$cond=="death"] <- "Full Sample"
 y$cond[y$cond=="Death"] <- "Full Sample"
 y$cond[y$cond=="all"] <- "Full Sample"
 
y <- reshape(y, direction  = "wide", timevar = "Stat", idvar = c("cond","sex","oc") )  
 y$value.n <- NULL

 y <- y[order(y$sex,y$cond,-y$value.N),]
 y$value.N <- prettyNum(y$value.N, big.mark = ",")
 y[,5:length(y)] <- round(y[,5:length(y)],4)
 y$value.R2 <- as.character(y$value.R2)
 y$value.aR2 <- as.character(y$value.aR2)
 y$value.mean <- as.character(y$value.mean)
 y$value.SD <- as.character(y$value.SD)
 
 ymeans <- y
 
 ym <- y[,3:length(y)]
 names(ym) <- c("Outcome","N","R2","aR2","Mean","SD")
 ym <- ym[,c("Outcome","N","Mean","SD","aR2","R2")]
 
 addtorow <- list()
 addtorow$pos <- list(0,0,1,5,9,10,14)
 addtorow$command <- c( paste0(paste(names(ym),collapse = " & "),"\\\\\n"), 
                        paste0(paste0("\\textbf{",ymeans$sex[1],"}"),"& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Full Sample}","} \\\\\n"),
                        paste0("& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Diabetes Sample}","} \\\\\n") ,
                        paste0("& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Hypertension Sample}","} \\\\\n") ,
                        paste0(paste0("\\textbf{",ymeans$sex[length(ymeans$sex)],"}"),"& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Full Sample}","} \\\\\n") ,
                        paste0("& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Diabetes Sample}","} \\\\\n") ,
                        paste0("& \\multicolumn{",length(ym )-1,"}{c}{","\\underline{Hypertension Sample}","} \\\\\n")  ) 
 print(xtable(ym),add.to.row = addtorow,include.colnames = FALSE,include.rownames = FALSE, file=file.path(dataIn,paste0("Table_LPMMeans.tex")))
 
 
